//
//  ZyaPurchaseDetailBottomView.swift
//  SlaughterClient
//
//  Created by hudan on 2025/11/17.
//  Copyright © 2025 zwb. All rights reserved.
//

import UIKit

final class ZyaPurchaseDetailBottomView: UIView {
    
    var orderBlock: (() -> Void)?
    
    var count = 0 {
        didSet {
            if count <= 0 {
                countLab.extHidden(hidden: true)
            } else {
                countLab.extHidden(hidden: false)
                countLab.text = "\(count)"
                let width = countLab.sizeThatFits(CGSize(width: Double.greatestFiniteMagnitude, height: 12)).width + 8
                countLab.snp.updateConstraints { make in
                    make.width.equalTo(width)
                }
            }
        }
    }
    
    var amount: String = "¥ 0" {
        didSet {
            amountLab.text = amount
        }
    }
    
    private let countLab = UILabel().easy(.white, .systemFont(ofSize: 12))
            .extBgColor(color: .red)
            .extTextAligment(aligment: .center)
            .extRadius(radius: 8).extMaskBounds(mask: true)
            .extHidden(hidden: true)
    
    private let amountLab = UILabel().easy(.systemRed, .systemFont(ofSize: 17, weight: .medium))
    
    private lazy var orderBtn: UIButton = {
        let btn = UIButton().easy("保存", .white, .systemFont(ofSize: 16, weight: .medium), nil, self, #selector(orderBtnClick))
            .extBgColor(color: Theme.primaryColor())
            .extRadius(radius: 18).extMaskBounds(mask: true)
        return btn
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() {
        extBgColor(color: .white)
        
        let iconView = UIImageView(image: .init(named: "cart_circle"))
            .extRadius(radius: 20).extMaskBounds(mask: true)
        addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(40)
        }
        
        addSubview(countLab)
        countLab.snp.makeConstraints { make in
            make.top.equalTo(iconView).offset(-4)
            make.width.height.equalTo(16)
            make.left.equalTo(iconView.snp.centerX).offset(4)
        }
        
        let amountTitleLab = UILabel().easy("合计", .black, .systemFont(ofSize: 15, weight: .medium))
        addSubview(amountTitleLab)
        amountTitleLab.snp.makeConstraints { make in
            make.left.equalTo(iconView.snp.right).offset(20)
            make.width.equalTo(32)
            make.height.equalTo(20)
            make.centerY.equalTo(iconView)
        }
        
        addSubview(orderBtn)
        orderBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 90, height: 36))
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalTo(iconView)
        }
        
        addSubview(amountLab)
        amountLab.snp.makeConstraints { make in
            make.left.equalTo(amountTitleLab.snp.right)
            make.right.equalTo(orderBtn.snp.left).offset(-10)
            make.height.equalTo(20)
            make.centerY.equalTo(amountTitleLab)
        }
    }
    
    @objc func orderBtnClick() {
        orderBlock?()
    }
}

